﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<ThietBiSo.LinQDB.SanPham>" %>

<%@ Import Namespace="ThietBiSo.LinQDB" %>
<%@ Import Namespace="ThietBiSo.Models" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Website - bảng điều khiển
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        function popupURL(url, width, height) {
            var left = (screen.width - width) / 2;
            var top = (screen.height - height) / 2;
            window.open(url, 'NapCoin', 'left=' + left + 'px,top=' + top + 'px,width=' + width + 'px,height=' + height + 'px,location=no,scrollbars=yes,toolbar=no,resizable=yes,')
        }
        function getUrlVars() {
            var vars = {};
            var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                vars[key] = value;
            });
            return vars;
        }

        $().ready(function () {
            var first = getUrlVars()["cbNhom"];
            //var seconds = getUrlVars()["dm"];
            $("#btnAdd").click(function () {
                if (checkForm()) {
                    location.href = '<%=Url.Action("Create","sanpham")%>' + "?cbNhom=" + first;
                }
            });

            $("#cbNhom").change(function () {
                location.href = '<%=Url.Action("Index")%>' + "?cbNhom=" + $("#cbNhom").val();
            });

          

        });

        function checkForm() {
            var flag = true;
            var F = document.forms[0];
            var cbNhom = document.getElementById("cbNhom").value;        

            if (cbNhom == null || cbNhom == 0 || cbNhom == "") {
                alert("Chưa chọn danh mục");               
                flag = false;

            }           
            return flag;
        }

        function sanpham_delete(ma) {
            var cnf = window.confirm("Bạn chắc chắn xóa?");
            if (cnf) {
                $.ajax({
                    type: "POST",
                    url: '<%=Url.Action("Delete") %>',
                    cache: false,
                    async: false,
                    dataType: "json",
                    data: { ma: ma },
                    success: function (result) {
                        if (result.length == 0) { // ko tồn tại                        
                            //                            alert("Xóa dữ liệu thành công.");
                            window.location.reload();
                        }
                        else {
                            alert(result);
                        }
                    }
                });
            }

        }
        function sanpham_hienthi(ma) {

            $.ajax({
                type: "POST",
                url: '<%=Url.Action("HienThi") %>',
                cache: false,
                async: false,
                dataType: "json",
                data: { ma: ma },
                success: function (result) {
                }
            });
        }
        function sanpham_trangchu(ma) {

            $.ajax({
                type: "POST",
                url: '<%=Url.Action("TrangChu") %>',
                cache: false,
                async: false,
                dataType: "json",
                data: { ma: ma },
                success: function (result) {
                }
            });
        }
    </script>
  
    <%
        MDanhMuc mldm = new MDanhMuc();
        var vldm = mldm.FindAll();
    %>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tr>
            <td>
                <table width="100%">
                    <tr>
                        <td align="left">
                            <%-- <%= Html.ActionLink("Thêm sản phẩm", "Create",null, new { @class = "classThem" })%>--%>
                            <input type="button" value="Thêm sản phẩm" id="btnAdd" />
                        </td>
                        <%--<td align="right">
                            <%: Html.ActionLink("Quay lại", "quantri", "Admin", null, new { @class = "" })%>
                        </td>--%>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="left">
                Chọn danh mục:
                <%: Html.DropDownList("cbNhom", new SelectList(vldm, "MaDanhMuc", "TenDanhMuc"), "Chọn danh mục", new { @style = "width:300px" })%>
            
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <table id="bang_admin" class="tbl colorize" width="100%" border="0">
                    <tbody>
                        <tr>
                          
                            <th align="center" width="20px">
                                TT
                            </th>
                             <th width="70">
                                Hình
                            </th>
                          
                            <th width="200">
                                Tên sản phẩm
                            </th>
                           
                            <th align="left" width="100">
                                Danh mục
                            </th>
                           
                          
                            <th width="20">
                                Xuất bản
                            </th>
                            <th width="20">
                                Trang chủ
                            </th>
                            <th width="20">
                                Xóa
                            </th>
                        </tr>
                    </tbody>
                    <tbody>
                        <% foreach (SanPham sp in (List<SanPham>)ViewData["sanphams"])
                           {               
                        %>
                        <tr class="tb_hienthi">
                            <%-- <td align="center" valign="middle">
                                <input type="checkbox" id="" class="" style="" />
                            </td>--%>
                            <td align="center" valign="middle">
                                <input type="text" disabled="disabled" autocomplete="off" class="orderbox" value="<%:sp.ThuTu %>"
                                    maxlength="4" id="thutu" style="width: 30px; text-align: center" />
                                <%:Html.Hidden("MaSPHidden",sp.MaSP) %>
                            </td>
                          
                            <td align="center" valign="middle">
                                <a href="<%=Url.Action("Edit","SanPham", new {id = sp.MaSP,cbNhom= sp.MaDM} ) %>">
                                    <%if (sp.HinhAnh == "" || sp.HinhAnh == null || sp.HinhAnh == "noImage")
                                      {%>
                                    <img src="<%:Url.Content("~/Content/images/nophoto.gif")%>" alt="" width="50px;"
                                        height="50px;" />
                                    <%}
                                      else
                                      { %>
                                    <img src="<%:sp.HinhAnh%>" alt="" width="50px;" height="50px;" />
                                    <%} %>
                                </a>
                            </td>
                            <td align="left">
                                <%: sp.TenSP%>
                            </td>
                          
                            <td>
                                <%if (sp.MaMDCon != null)
                                  { %>
                                <%:sp.DanhMucCon.TenDMCon%>
                                <% }
                                  else
                                  {%>
                                <%:sp.DanhMuc.TenDanhMuc%>
                                <%}%>
                            </td>
                           
                            <td align="center">
                                <%if (sp.HienThi == false)
                                  {
                                %>
                                <a href="javascript:sanpham_hienthi(<%:sp.MaSP%>);">
                                    <input type="checkbox" />
                                </a>
                                <%}
                                  else
                                  {%>
                                <a href="javascript:sanpham_hienthi(<%:sp.MaSP%>);">
                                    <input type="checkbox" checked="checked" /></a>
                                <%} %>
                            </td>
                            <td align="center">
                                <%if (sp.TrangChu == false)
                                  {
                                %>
                                <a href="javascript:sanpham_trangchu(<%:sp.MaSP%>);">
                                    <input type="checkbox" />
                                </a>
                                <%}
                                  else
                                  {%>
                                <a href="javascript:sanpham_trangchu(<%:sp.MaSP%>);">
                                    <input type="checkbox" checked="checked" /></a>
                                <%} %>
                            </td>
                          
                            <td align="center">
                                <a href="javascript:sanpham_delete(<%:sp.MaSP %>);">
                                    <img src="../../Content/imagesAdmin/images/pic_trash.gif" alt="" /></a>
                            </td>
                            
                        </tr>
                        <% } %>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td style="text-align: center; clear: both; width: 100%; padding: 20px 0 10px 0;">
                <% if ((string)ViewData["pageLinks"] != "")
                   { %>
                <%= ViewData["pageLinks"] %>
                <% } %>Tổng sản phẩm: <strong>
                    <%= ViewData["total"]%></strong>
            </td>
        </tr>
    </table>
</asp:Content>
